<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta http-equiv="Content-Style-Type" content="text/css">
  <title></title>
  <meta name="Generator" content="Cocoa HTML Writer">
  <meta name="CocoaVersion" content="824.41">
  <style type="text/css">
    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
    p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica; min-height: 22.0px}
    p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
    p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
    p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
    p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
    p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
    span.s1 {color: #000000}
    span.s2 {color: #0000bf}
    span.s3 {color: #bf0000}
    span.s4 {color: #007300}
    span.Apple-tab-span {white-space:pre}
  </style>
</head>
<body>
<p class="p1">//SLUGens released under the GNU GPL as extensions for SuperCollider 3, by Nick Collins, http://www.informatics.sussex.ac.uk/users/nc81/</p>
<p class="p2"><b></b><br></p>
<p class="p3"><b>GravityGrid2<span class="Apple-tab-span">	</span><span class="Apple-tab-span">	</span>dynamical system simulation (Newtonian gravitational force)</b></p>
<p class="p2"><b></b><br></p>
<p class="p4"><b>GravityGrid2.ar(reset, rate, newx, newy, bufnum, mul, add)</b></p>
<p class="p5"><br></p>
<p class="p4">Fixed masses apply Newtonian gravitational force dynamics to a central moving mass which cannot escape the [-1, 1] grid in x or y. The position of the moving mass is sonified as an oscillator by its distance from the centre.<span class="Apple-converted-space">   </span></p>
<p class="p5"><br></p>
<p class="p4">This is a relatively expensive oscillator to run.<span class="Apple-converted-space"> </span></p>
<p class="p5"><br></p>
<p class="p4"><b>reset</b>- Restart the moving mass at a random position within the square (k-rate input)</p>
<p class="p4"><b>rate</b>- amount of position update per sample (k-rate)</p>
<p class="p4"><b>newx</b>- kr input to be sampled for new x positions for the moving mass on reset</p>
<p class="p4"><b>newy</b>- kr input to be sampled for new y positions for the moving mass on reset</p>
<p class="p4"><b>bufnum</b>- Bufnum for a buffer containing weights and positions for the fixed influencing masses. In the format entry [0] is the number of masses, then 3 components (x, y, mass multiplier) for each mass in turn (see below). You can dynamically change this buffer as long as the data contents stay consistent- i.e. if you change suddenly to having twice as many masses, make sure you've provided x,y and weight values for them!<span class="Apple-converted-space"> </span></p>
<p class="p5"><span class="Apple-converted-space"> </span></p>
<p class="p6"><br></p>
<p class="p1"><span class="s1">b = </span><span class="s2">Buffer</span><span class="s1">.alloc(s, 1+(25*3), 1); </span>//mass buffer big enough for up to 25 masses</p>
<p class="p6"><br></p>
<p class="p7">b.setn(0,[5]++(<span class="s2">Array</span>.fill(5,{[1.0.rand2,1.0.rand2,rrand(0.1,1.0)]}).flatten)); <span class="s3">//set weights</span></p>
<p class="p6"><br></p>
<p class="p7">{<span class="s2">Pan2</span>.ar(<span class="s2">GravityGrid2</span>.ar(<span class="s2">Impulse</span>.kr(1),<span class="s2">MouseX</span>.kr(0.1,1,<span class="s4">'exponential'</span>),<span class="s2">LFNoise0</span>.kr(1,0.8),<span class="s2">LFNoise0</span>.kr(1,0.8), b.bufnum),0.0)}.play</p>
<p class="p6"><br></p>
<p class="p7">(</p>
<p class="p7"><span class="s2">var</span> n;</p>
<p class="p6"><br></p>
<p class="p1"><span class="s1">n=rrand(1,20); </span>//random number of masses</p>
<p class="p6"><br></p>
<p class="p7">b.setn(0,[n]++(<span class="s2">Array</span>.fill(n,{[1.0.rand2,1.0.rand2,rrand(0.1,1.0)]}).flatten)); <span class="s3">//change weights to new random values</span></p>
<p class="p7">)</p>
<p class="p6"><br></p>
<p class="p6"><br></p>
<p class="p7">{<span class="s2">Pan2</span>.ar(<span class="s2">GravityGrid2</span>.ar(<span class="s2">Impulse</span>.kr(<span class="s2">MouseY</span>.kr(1,1000,<span class="s4">'exponential'</span>)),<span class="s2">MouseX</span>.kr(0.1,1,<span class="s4">'exponential'</span>),<span class="s2">LFNoise0</span>.kr(1,0.8),<span class="s2">LFNoise0</span>.kr(1,0.8), b.bufnum),0.0)}.play</p>
<p class="p6"><br></p>
<p class="p6"><br></p>
<p class="p6"><br></p>
<p class="p1">//lower gravity</p>
<p class="p7">(</p>
<p class="p7"><span class="s2">var</span> n;</p>
<p class="p6"><br></p>
<p class="p1"><span class="s1">n=rrand(1,20); </span>//random number of masses</p>
<p class="p6"><br></p>
<p class="p7">b.setn(0,[n]++(<span class="s2">Array</span>.fill(n,{[1.0.rand2,1.0.rand2,0.01*rrand(0.1,1.0)]}).flatten)); <span class="s3">//change weights to new random values</span></p>
<p class="p7">)</p>
<p class="p6"><br></p>
<p class="p6"><br></p>
<p class="p7">{<span class="s2">Pan2</span>.ar(<span class="s2">GravityGrid2</span>.ar(<span class="s2">Impulse</span>.kr(0),<span class="s2">MouseX</span>.kr(0.1,10,<span class="s4">'exponential'</span>),<span class="s2">LFNoise0</span>.kr(1,0.8),<span class="s2">LFNoise0</span>.kr(1,0.8), b.bufnum),0.0)}.play</p>
<p class="p6"><br></p>
<p class="p6"><br></p>
<p class="p7">(</p>
<p class="p1">//deliberate corners</p>
<p class="p7">b.setn(0,[4]++(<span class="s2">Array</span>.fill(4,{<span class="s2">arg</span> i; [if(i&lt;2,1,-1),if(i%2==0,1,-1),0.1*rrand(0.1,1.0)]}).flatten)); <span class="s3">//set weights</span></p>
<p class="p7">)</p>
<p class="p6"><br></p>
<p class="p6"><br></p>
<p class="p7">(</p>
<p class="p1">//deliberate corners further away</p>
<p class="p7">b.setn(0,[4]++(<span class="s2">Array</span>.fill(4,{<span class="s2">arg</span> i; [if(i&lt;2,rrand(1,2.3),-1),if(i%2==0,1,rrand(-1.1,-1.5)),0.1*rrand(0.1,1.0)]}).flatten)); <span class="s3">//set weights</span></p>
<p class="p7">)</p>
<p class="p6"><br></p>
<p class="p6"><br></p>
<p class="p6"><br></p>
<p class="p1">//very fun and noisy!</p>
<p class="p7">{<span class="s2">Pan2</span>.ar(<span class="s2">GravityGrid2</span>.ar(<span class="s2">Impulse</span>.kr(0),<span class="s2">MouseX</span>.kr(1,1000,<span class="s4">'exponential'</span>),<span class="s2">LFNoise0</span>.kr(1,0.8),<span class="s2">LFNoise0</span>.kr(1,0.8), b.bufnum),0.0)}.play</p>
<p class="p6"><br></p>
<p class="p6"><br></p>
<p class="p1">//if something fun turns up in the four mass version</p>
<p class="p7">b.getn(0,13,{<span class="s2">arg</span> in; in.postln;})</p>
<p class="p6"><br></p>
<p class="p1">//here's one I made earlier</p>
<p class="p7">b.setn(0, [ 4, 1, 1, 0.029076481238008, 1, -1, 0.061766054481268, -1, 1, 0.096376851201057, -1, -1, 0.09320655465126 ])</p>
<p class="p6"><br></p>
<p class="p1">//and another</p>
<p class="p7">b.setn(0, [ 4, 1.4228951931, 1, 0.080506414175034, 1.3617297410965, -1.3782749176025, 0.027821443974972, -1, 1, 0.038790885359049, -1, -1.3663036823273, 0.047782249748707 ])</p>
<p class="p6"><br></p>
<p class="p6"><br></p>
<p class="p6"><br></p>
<p class="p6"><br></p>
<p class="p1">//higher gravity but larger placement area outside box which constrains the moving particle</p>
<p class="p7">(</p>
<p class="p7"><span class="s2">var</span> n;</p>
<p class="p6"><br></p>
<p class="p1"><span class="s1">n=rrand(1,20); </span>//random number of masses</p>
<p class="p6"><br></p>
<p class="p7">b.setn(0,[n]++(<span class="s2">Array</span>.fill(n,{[5.0.rand2,5.0.rand2,exprand(0.1,100.0)]}).flatten)); <span class="s3">//change weights to new random values</span></p>
<p class="p7">)</p>
<p class="p6"><br></p>
<p class="p6"><br></p>
<p class="p6"><br></p>
<p class="p6"><br></p>
<p class="p1">//noise piece</p>
<p class="p7">{<span class="s2">Resonz</span>.ar(<span class="s2">GravityGrid2</span>.ar(<span class="s2">Impulse</span>.kr(<span class="s2">LFNoise0</span>.kr([0.2,0.3],7,8)),<span class="s2">MouseX</span>.kr(0.1,10)*[11.2,12.5], <span class="s2">LFNoise0</span>.kr(5.4,0.4),<span class="s2">LFNoise0</span>.kr(10,0.8), b.bufnum),<span class="s2">SinOsc</span>.ar(0.03,0,2000,3000),<span class="s2">SinOsc</span>.ar(0.03,0,0.3,0.4))}.play</p>
<p class="p6"><br></p>
<p class="p6"><br></p>
</body>
</html>
